#include "bitypedefs.hpp"
#include "alias.hpp"
#include "lexer.hpp"
#include "builtins.hpp"

Variable bi_var(vector<Variable>& argv, Environment& env)
{
	unsigned argc = argv.size();
	if(argc == 1) throw ERROR_ARGUMENTCOUNT;
	for(unsigned i=1;i<argv.size();i++) {
		Variable &tmp = argv[i];
		if(tmp.type != tVarname) throw ERROR_ARGUMENTS;
		env.getvar(tmp.str) = Variable();
	}
	return Variable();
}

Variable bi_typedef(vector<Variable>& argv, Environment& env)
{
	(void)env;
	unsigned argc = argv.size();
	if(argc != 3) {
		throw_error(ERROR_ARGUMENTCOUNT);
	}
	Variable &type_name = argv[1];
	if(type_name.type != tVarname) throw ERROR_ARGUMENTS;
	Variable &code = argv[2];
	env.verify(code);
	if(code.type != tInline) throw ERROR_ARGUMENTS;
	Environment tenv; // This env will hold bi_var and bi_alias
	tenv._var.clear();
	tenv._var.push_back(map<string,Variable>());
	tenv.add("var",bi_var);
	tenv.add("alias",bi_alias);
	tenv.add("null",bi_null);
	orate(code.str, tenv, DISABLESCOPE);
	tenv._var[0].erase("__return__");
	for(map<string,Variable>::iterator it = tenv._var[0].begin();it != tenv._var[0].end();it++) {
		if(it->second.type != tAlias) {
			env.types[type_name.str].add(it->first);
		}
	}
	env.types[type_name.str].alias = tenv._alias;
	return code;
}
